package ig.metrica.ui.conexion;


import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ig.metrica.ui.PantallaSevinproPanel;
import ig.util.ConexionServer;
import ig.util.ejb.IGBean;
import ig.util.testConexion;
import ig.util.testConexionHome;
import java.awt.Frame;
import java.sql.DriverManager;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;

public class PantallaDatosBasicosConexion extends PantallaSevinproPanel {
    
    private JTextField txtNombre;
    private JTextField txtDriver;
    private JTextField txtURL;
    private JTextField txtUsuario;
    private JPasswordField txtPassword;
    private JLabel usuario=new JLabel("Usuario");
    private JLabel clave=new JLabel("Password");    
    public Frame frame=null;

    private JComboBox[] comboBoxsColumns=new JComboBox[1];

    @Override
    public JComboBox[] getComboBoxsColumnsTable1() {
        return comboBoxsColumns;
    }

    private JComboBox[] comboBoxsColumnsTable2=new JComboBox[1];
    @Override
    public JComboBox[] getComboBoxsColumnsTable2() {
        return comboBoxsColumnsTable2;
    }

    public PantallaDatosBasicosConexion(Frame frame){
        this.frame=frame;
        JPanel panelSuperior;
        
        setLayout( new BorderLayout() );
        
        //int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, int anchor, int fill, Insets insets, int ipadx, int ipady
        
        add( panelSuperior = new JPanel(), BorderLayout.NORTH );
        panelSuperior.setLayout( new GridBagLayout() );
        panelSuperior.setBorder( BorderFactory.createEmptyBorder(10, 10, 0, 10) );
        //----------
        panelSuperior.add( new JLabel(), new GridBagConstraints(0, 0, 1, 1, 0.4, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( new JLabel("Nombre"), new GridBagConstraints(1, 0, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( new JLabel("Driver"), new GridBagConstraints(1, 1, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( new JLabel("URL de Conexion"), new GridBagConstraints(1, 2, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( usuario, new GridBagConstraints(1, 3, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( clave, new GridBagConstraints(1, 4, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( new JLabel(), new GridBagConstraints(2, 0, 1, 1, 0.1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( txtNombre = new JTextField(), new GridBagConstraints(3, 0, 1, 1, 1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( txtDriver = new JTextField(), new GridBagConstraints(3, 1, 1, 1, 1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( txtURL = new JTextField(), new GridBagConstraints(3, 2, 1, 1, 1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( txtUsuario = new JTextField(), new GridBagConstraints(3, 3, 1, 1, 1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( txtPassword = new JPasswordField(), new GridBagConstraints(3, 4, 1, 1, 1, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        panelSuperior.add( new JLabel(), new GridBagConstraints(4, 0, 1, 1, 0.4, 0.1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0) );
        txtNombre .setDisabledTextColor(new java.awt.Color(0, 0, 0));
        txtDriver .setDisabledTextColor(new java.awt.Color(0, 0, 0));
        txtURL .setDisabledTextColor(new java.awt.Color(0, 0, 0));
        txtUsuario .setDisabledTextColor(new java.awt.Color(0, 0, 0));
        txtPassword .setDisabledTextColor(new java.awt.Color(0, 0, 0));
        JButton test=new JButton("Probar");
//            test.setBorder(null);
        add(test,BorderLayout.SOUTH);
        
        test.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    Test(evt);
                } catch (CreateException ex) {
                    Logger.getLogger(PantallaDatosBasicosConexion.class.getName()).log(Level.SEVERE, null, ex);
                } 
            }
        });
//            JButton test;
//            add(test= new JButton("Tset"),BorderLayout.SOUTH);
    }
    private void Test(java.awt.event.ActionEvent evt) throws CreateException{

        try {

            Properties prop = new Properties();
            prop.put( Context.INITIAL_CONTEXT_FACTORY, IGBean.INITIAL_CONTEXT_FACTORY);

            String Sockec = new ConexionServer().getSocketServer();
            prop.put( Context.PROVIDER_URL, Sockec);
            InitialContext ctx = new InitialContext(prop);

            testConexionHome testConexionHome = (testConexionHome)ctx.lookup("testConexionBean");
            testConexion testConexion = (testConexion)testConexionHome.create();
            int res = testConexion.probar(  this.txtDriver.getText().trim(),
                                            this.txtURL.getText().trim(),
                                            this.txtUsuario.getText().trim(),
                                            this.txtPassword.getText().trim());

            if(res == 1)
                JOptionPane.showMessageDialog(this.frame,"La Conexi\u00F3n funciona correctamente.");
            else if(res == 2)
                JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el Driver\n","Mensaje",JOptionPane.ERROR_MESSAGE);
            else
                JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el URL, Usuario y Password\n","Mensaje",JOptionPane.ERROR_MESSAGE);

        } catch (RemoteException ex) {
            Logger.getLogger(PantallaDatosBasicosConexion.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            Logger.getLogger(PantallaDatosBasicosConexion.class.getName()).log(Level.SEVERE, null, ex);
        }


        /*Connection con1=null;
        try {
            Class.forName(this.txtDriver.getText().trim());
            con1= DriverManager.getConnection(  this.txtURL.getText().trim(),
                                                this.txtUsuario.getText().trim(),
                                                this.txtPassword.getText().trim());
            
            JOptionPane.showMessageDialog(this.frame,"La Conexi\u00F3n funciona correctamente.");
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el Driver\n"+e.toString(),"Mensaje",JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el URL, Usuario y Password\n"+e.toString(),"Mensaje",JOptionPane.ERROR_MESSAGE);
        }finally {
            try {con1.close();} catch (Exception e) {}
        }*/
         

        /*if(message[0].equals("0")){
            JOptionPane.showMessageDialog(this.frame,"La Conexi\u00F3n funciona correctamente.");
        }else if(message[0].equals("1")){
            JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el Driver\n"+message[1],"Mensaje",JOptionPane.ERROR_MESSAGE);
        }else if(message[0].equals("2")){
            JOptionPane.showMessageDialog(this.frame,"Error de conexi\u00F3n: Verifique el URL, Usuario y Password\n"+message[1],"Mensaje",JOptionPane.ERROR_MESSAGE);
        }*/
    }
    public String getTitulo() {
        return "Datos Basicos de Conexion";
    }
    
    public void setModificable(boolean b) {
        txtNombre.setEnabled(b);
        txtDriver.setEnabled(b);
        txtURL.setEnabled(b);
        txtUsuario.setEnabled(b);
        txtPassword.setEnabled(b);
    }
    
    public void limpiarCampos() {
        txtNombre.setText("");
        txtDriver.setText("");
        txtURL.setText("");
        txtUsuario.setText("");
        txtPassword.setText("");
        repaint();
    }
    
    public String getNombre(){
        return txtNombre.getText();
    }
    
    public String getDriver(){
        return txtDriver.getText();
    }
    
    public String getURL(){
        return txtURL.getText();
    }
    
    public String getUsuario(){
        return txtUsuario.getText();
    }
    
    public String getPassword(){
        return new String(txtPassword.getPassword());
    }
    
    
    public void setNombre(String nombre){
        txtNombre.setText(nombre);
        this.updateUI();
    }
    
    public void setDriver(String driver){
        txtDriver.setText(driver);
        this.updateUI();
    }
    
    public void setURL(String url){
        txtURL.setText(url);
        this.updateUI();
    }
    
    public void setUsuario(String usuario){
        txtUsuario.setText(usuario);
        this.updateUI();
    }
    
    public void setPassword(String password){
        txtPassword.setText(password);
        this.updateUI();
    }
    
    public void salir() throws Exception {
    }
    public void setNameLable(String user,String pass){
        if(user!=null && pass!=null){
            clave.setText(pass);
            usuario.setText(user);
//            this.setUsuario(";");
//            this.setPassword(".TXT");
        }
    }

    public javax.swing.table.AbstractTableModel getClonModelTable_1() throws CloneNotSupportedException{
        return null;
    }

    public javax.swing.table.AbstractTableModel getClonModelTable_2() throws CloneNotSupportedException{
        return null;
    }

    public javax.swing.table.AbstractTableModel getClonModelTable_3() throws CloneNotSupportedException{
        return null;
    }

}